package com.futurescale.sa.model.vo
{
	public class PartVO extends ValueObject
	{
		/**
		 * The XML element name.
		 */
		public static const ELEMENT:String = "Part";
		
		/**
		 * Construct a PartVO. 
		 */
		public function PartVO( xml:XML=null )
		{
			super( ELEMENT, xml );
			if (xml == null) getNewChapter();
		}
		
		/**
		 * Get a new Chapter for the Story. 
		 */
		public function getNewChapter(  ):ChapterVO
		{
			var chapter:ChapterVO = new ChapterVO();
			addChapter( chapter );
			return chapter;
		}
		
		/**
		 * Add a Chapter to the Part.
		 * Renumbers the existing Chapters if the added Chapter 
		 * already has an ordinal less than the next Chapter 
		 * ordinal. Otherwise next Chapter ordinal is assigned.
		 * 
		 * Forces the story type to NORMAL if no part argument
		 */
		public function addChapter( chapter:ChapterVO ):void
		{
			// renumber chapters if inserting
			if ( chapter.ordinal > 0 && chapter.ordinal < nextChapterOrdinal ) {
				var chapters:Vector.<ChapterVO> = this.chapters;
				for each ( var cvo:ChapterVO in chapters ){
					if ( cvo.ordinal >= chapter.ordinal ) cvo.ordinal++;
				}
			} else {
				chapter.ordinal = nextChapterOrdinal;
			}
			xml.appendChild( chapter.xml );
		}
		
		
		/** 
		 * Delete a Chapter from the Part, renumbering
		 */
		public function deleteChapter( chapter:ChapterVO  ):void
		{
			var chapterOrd:Number = chapter.ordinal;
			delete xml..Chapter.(@uid == chapter.uid)[0];
			var chapters:Vector.<ChapterVO> = this.chapters;
			for each ( var cvo:ChapterVO in chapters ){
				if ( cvo.ordinal > chapterOrd ) cvo.ordinal--;
			}
		}
		
		/** 
		 * An ordinal-sorted Vector of this Part's Chapters.
		 */
		public function get chapters():Vector.<ChapterVO>
		{
			var v:Vector.<ChapterVO> = new Vector.<ChapterVO>();
			var xl:XMLList = xml.child(ChapterVO.ELEMENT);
			for each ( var cx:XML in xl ) {
				var chapter:ChapterVO = new ChapterVO(cx);
				v.push(chapter);
			}
			v.sort(ValueObject.ordinalSort);
			return v;
		}
		public function set chapters( v:Vector.<ChapterVO> ):void
		{} // read-only but bindable
		
		/**
		 * Get the text of the current Draft of all Scenes
		 * in each Chapter of this Part. The Part name 
		 * is included at the beginning of the text.
		 */
		public function getText( includeHeading:Boolean ):String
		{
			var t:String = includeHeading ? name + DOUBLE_SPACE : "";
			var chapterList:Vector.<ChapterVO> = chapters;
			if (chapterList.length > 1) {
				// multiple chapters with headings
				for each ( var cvo:ChapterVO in chapterList) {
					t += cvo.getText(true) + DOUBLE_SPACE;
				}
			} else if (chapterList.length == 1) {
				// single chapter sans heading
				t += ChapterVO(chapterList[0]).getText(false);
			}
			
			// read-only text of part
			return t;
		}
		
		/**
		 * Get the next Chapter ordinal for this Story.
		 * For NORMAL stories with Scenes and Chapters.
		 */
		private function get nextChapterOrdinal():Number
		{
			return getNextOrdinal( xml..Chapter );
		}
		
	}
}